﻿/*----------------------------------------------------------------
// Copyright (C) 2009 
// 版权所有。 
//
// 文件名：
// 文件功能描述：设备保管信息登记
//
//
// 创建标识：
// 程序作者：翟允锋
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Dal;
using System.Threading;
using EQT.Model;
using Wuqi.Webdiyer;


public partial class EquipKeptReg : System.Web.UI.Page
{
    private static readonly Common.Logging.ILog log = Common.Logging.LogManager.GetCurrentClassLogger();
    private DaKeptLog dalKept = DevFuture.Common.WorkContext.AppCtx.GetObject("DaKeptLog") as DaKeptLog;
    private DaDept dalDept = DevFuture.Common.WorkContext.AppCtx.GetObject("DaDept") as DaDept;
    private DaEmployee dalEmp = DevFuture.Common.WorkContext.AppCtx.GetObject("DaEmployee") as DaEmployee;
    private UserContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        context = Session["USER_CONTEXT"] as UserContext;
        OperationResult.Text = "";
        divResult.Visible = false;
        if (!IsPostBack)
        {
            DataSet dsDept = dalDept.Query(string.Empty);

            Utility.BindDataSet2Combox(dsDept, CreateDdlDeptID1, "Dept_Name", "Dept_ID");
            Utility.BindDataSet2Combox(dsDept, CreateDdlDeptID2, "Dept_Name", "Dept_ID");

            CreateDdlDeptID1.Items.Insert(0, new ListItem("", ""));
            CreateDdlDeptID2.Items.Insert(0, new ListItem("", ""));


            if (!string.IsNullOrEmpty(Request.QueryString["EquipID"]))
            {
                CreateEquip_ID.Text = Request.QueryString["EquipID"].ToString();

                MoKeptLog mod = dalKept.GetEquipLastKept(Request.QueryString["EquipID"].ToString());

                if (mod != null)
                {
                    MoDept moddept = dalEmp.GetEmpDepartment(mod.UserCode2);
                    CreateDdlDeptID1.SelectedValue = moddept == null ? "" : moddept.DeptId;

                    this.CreateDdlDeptID1_SelectedIndexChanged(null, null);

                    if (CreateUser_Code1.Items.FindByValue(mod.UserCode2) != null)
                    {
                        CreateUser_Code1.SelectedValue = mod.UserCode2;
                    }
                    CreateDdlDeptID1.Enabled = false;
                    CreateUser_Code1.Enabled = false;
                    LastKeptID.Value = mod.KptLogId.ToString();
                }
                CreateEquip_ID.Enabled = false;
            }
        }

    }

    protected void CreateDdlDeptID1_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateUser_Code1.Items.Clear();

        string deptid = CreateDdlDeptID1.SelectedValue;
        if (!string.IsNullOrEmpty(deptid))
        {
            Utility.BindDataSet2Combox(dalEmp.GetEmploreeByDeptID(deptid), CreateUser_Code1, "FullName", "Emp_Id");
            CreateUser_Code1.Items.Insert(0, new ListItem("", ""));
        }
    }

    protected void CreateDdlDeptID2_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateUser_Code2.Items.Clear();

        string deptid = CreateDdlDeptID2.SelectedValue;
        if (!string.IsNullOrEmpty(deptid))
        {
            Utility.BindDataSet2Combox(dalEmp.GetEmploreeByDeptID(deptid), CreateUser_Code2, "FullName", "Emp_Id");
        }
    }

    protected void CreateGo_Click(object sender, EventArgs e)
    {
        MoKeptLog mod = new MoKeptLog();

        try
        {
            //mod.KptLogId = SequenceKeys.GenerateSampleKeptID();
            mod.EquipId = CreateEquip_ID.Text.Trim();
            mod.KptAddr = CreateKpt_Addr.Text.Trim();
            mod.KptBegin = DateTime.Now;
            mod.KptStatus = 1;
            mod.Rem = CreateRem.Text.Trim();

            if (!string.IsNullOrEmpty(CreateUser_Code1.SelectedValue))
            {
                mod.UserCode1 = CreateUser_Code1.SelectedValue;
            }
            mod.UserCode2 = CreateUser_Code2.SelectedValue;

            int rtn = dalKept.CreateKeptLog(mod, LastKeptID.Value);
            if (rtn == 1)
            {
                OperationResult.Text = "维修记录登记成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "维修记录登记失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("维修记录登记失败", ex);
            OperationResult.Text = "维修记录登记用户失败！"; divResult.Visible = true;
        }
    }
    protected void BtnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Equipment.aspx");
    }
}
